//
//  CMTableViewController.m
//  CaesarMusic
//
//  Created by Caesar on 15/10/30.
//  Copyright © 2015年 Caesar. All rights reserved.
//

#import "CMTableViewController.h"
#import "CMMusic.h"
#import "MJExtension.h"
#import "UIImage+Circle.h"
#import "CMPlayingViewController.h"
#import "CMMusicTool.h"

@interface CMTableViewController ()

/// 所有音乐
@property (nonatomic, strong) NSArray *musics;

@property (nonatomic, strong) CMPlayingViewController *playingView;

@end

@implementation CMTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 改变 cell 的高度
    self.tableView.rowHeight = 80;
}

#pragma mark - ***************> 懒加载 <***************
- (NSArray *)musics {
    if (_musics == nil) {
        self.musics = [CMMusic objectArrayWithFilename:@"Musics.plist"];
    }
    return _musics;
}

- (CMPlayingViewController *)playingView{
    
    if (_playingView == nil) {
        _playingView = [[CMPlayingViewController alloc]init];
    }
    return _playingView;
}


#pragma mark - Table view data source

// dang 选中某一个 cell 的时候会自动调用此方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 1.让 cell 变成不被选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CMMusic *music = [CMMusicTool musics][indexPath.row];
    
    [CMMusicTool setPlayingMusic:music];
    
    // 2.弹出界面
    [self.playingView show];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.musics.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"MiscCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }

//    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
    // 1.获取模型对象
    CMMusic *music = [CMMusicTool musics][indexPath.row];

    // 2.给 cell 赋值
    cell.imageView.image = [UIImage circleImageWithName:music.singerIcon    borderWidth:2.0 borderColor:[UIColor purpleColor]];
    cell.textLabel.text = music.name;
    cell.detailTextLabel.text = music.singer;

    return cell;
}





@end
